package com.siruier.boss.ui.activity.user

import android.view.View
import android.widget.TextView
import androidx.recyclerview.widget.DividerItemDecoration
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BasePopupView
import com.lxj.xpopupext.listener.TimePickerListener
import com.lxj.xpopupext.popup.TimePickerPopup
import com.siruier.boss.R
import com.siruier.boss.api.AreaIncomeLogBean
import com.siruier.boss.api.UserIntegralBean
import com.siruier.boss.api.core.launchUI
import com.siruier.boss.api.core.toData
import com.siruier.boss.api.loginApi
import com.siruier.boss.common.formatPrice
import com.siruier.boss.common.handleLoginName
import com.siruier.boss.databinding.ActivityAreaIncomeDetailBinding
import com.siruier.boss.databinding.ItemIncomeDetailBinding
import com.siruier.boss.ui.base.BaseActivity
import com.siruier.boss.ui.base.BaseRecyclerAdapter
import com.siruier.boss.ui.cache.UserCache
import com.siruier.boss.ui.ext.*
import com.siruier.boss.ui.helper.RefreshHelper
import com.siruier.boss.ui.utils.Utils
import java.util.*


class AreaIncomeDetailActivity : BaseActivity<ActivityAreaIncomeDetailBinding>(), TimePickerListener {
    private lateinit var mRefreshHelper: RefreshHelper<AreaIncomeLogBean>
    private val beginTimePopup: BasePopupView by lazy {
        XPopup.Builder(this).asCustom(
            TimePickerPopup(this)
                .setTimePickerListener(this)
        )
    }
    private val endTimePopup: BasePopupView by lazy {
        XPopup.Builder(this).asCustom(
            TimePickerPopup(this)
                .setTimePickerListener(this)
        )
    }
    private val mAreaIncomeDetailAdapter = AreaIncomeDetailAdapter()
    private var mTimeView: TextView? = null
    override fun initView() {
        Utils.invasionStatusBar(this)
        Utils.setDefaultStatusBarFont(this, false)
        bindViewClick(vb.tvBeginTime, vb.tvEndTime, vb.buttonSearch)
        vb.rvDetail.addItemDecoration(DividerItemDecoration(this, DividerItemDecoration.VERTICAL))
        mRefreshHelper = RefreshHelper(vb.refreshLayout, vb.rvDetail, mAreaIncomeDetailAdapter, listener = ::onReload)
        mRefreshHelper.onRefresh()
    }

    override fun initData() {
        UserCache.getBalanceTotal().apply {
            val totalMarketIntegral = totalMarketIntegral.formatPrice()
            val marketIntegral = marketIntegral.formatPrice()
            val usedMarketIntegral = usedMarketIntegral.formatPrice()
            vb.tvTotalMoney.textSpan("累计贡献值(￥)\n${totalMarketIntegral}") { changeMoneyTextStyle(totalMarketIntegral, this) }
            vb.tvWithdrawalAmount.textSpan("待使用贡献值(￥)\n${marketIntegral}") { changeMoneyTextStyle(marketIntegral, this) }
            vb.tvOutWithdrawalAmount.textSpan("已使用贡献值(￥)\n${usedMarketIntegral}") { changeMoneyTextStyle(usedMarketIntegral, this) }
        }
    }

    private fun changeMoneyTextStyle(spanText: String, span: TextViewSpan) {
        span.spanText = spanText
        span.addBoldSpan()
        span.addForegroundColorSpan(R.color.main_color.toColor())
        span.addAbsoluteSizeSpan(18.dp)
    }

    override fun onClick(v: View?) {
        when (v) {
            vb.tvBeginTime -> {
                mTimeView = vb.tvBeginTime
                beginTimePopup.show()
            }
            vb.tvEndTime -> {
                mTimeView = vb.tvEndTime
                endTimePopup.show()
            }
            vb.buttonSearch -> onReload()
        }
    }

    private fun onReload() {
        launchUI({
            loginApi.areaIncomeLog(vb.tvBeginTime.value, vb.tvEndTime.value, mRefreshHelper.page).toData()?.records
        }, mRefreshHelper.handApiObserver())
    }

    override fun onTimeChanged(date: Date?) {
    }

    override fun onTimeConfirm(date: Date, view: View?) {
        mTimeView?.text = date.format()
    }

    class AreaIncomeDetailAdapter : BaseRecyclerAdapter<ItemIncomeDetailBinding, AreaIncomeLogBean>() {

        override fun onBindViewHolder(vb: ItemIncomeDetailBinding, bean: AreaIncomeLogBean, position: Int) {
            vb.apply {
                tvAddTime.text = bean.addDate.ifBlank {
                    if (bean.addTime.isNotEmpty()) {
                        bean.addTime.split("T")[0]
                    } else {
                        ""
                    }
                }
                tvPhone.text = handleLoginName(bean.phone)
                tvChangeRemark.text = "${bean.changeRemark}${
                    if (bean.incomeType == 1 && bean.num > 0)
                        "+"
                    else if (bean.num > 0)
                        "-"
                    else
                        ""
                }${bean.num}"
            }
            setViewClick(position, vb.root)
        }
    }
}